<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Bayesian Network: bayesian/algorithm.hpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Bayesian Network
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_4922399365a46b010edf8e4eee2a5dfb.html">bayesian</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">algorithm.hpp</div>  </div>
</div><!--header-->
<div class="contents">
<a href="algorithm_8hpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="preprocessor">#ifndef BAYESIAN_NETWORKS_ALGORITHM_HPP</span></div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="preprocessor">#define BAYESIAN_NETWORKS_ALGORITHM_HPP</span></div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="preprocessor">#include &lt;functional&gt;</span></div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="preprocessor">#include &lt;vector&gt;</span></div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="preprocessor">#include &lt;<a class="code" href="network_8hpp.html">bayesian/network.hpp</a>&gt;</span></div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="preprocessor">#include &lt;<a class="code" href="component_8hpp.html">bayesian/network/component.hpp</a>&gt;</span></div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;</div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespacebn.html">bn</a> {</div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;</div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;<span class="keyword">template</span>&lt;<span class="keyword">class</span> F&gt;</div><div class="line"><a name="l00025"></a><span class="lineno"><a class="line" href="algorithm_8hpp.html#ab6d3fea4f9b73d4d4b75792c8dac735b">   25</a></span>&#160;<span class="keyword">auto</span> <a class="code" href="algorithm_8hpp.html#ab6d3fea4f9b73d4d4b75792c8dac735b">recursive</a>(F f)</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;{</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;    <span class="keywordflow">return</span> [f](<span class="keyword">auto</span>... a) { <span class="keywordflow">return</span> f(f, a...); };</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;}</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;</div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;</div><div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;<span class="keyword">template</span>&lt;<span class="keyword">class</span> RepresentMethod&gt;</div><div class="line"><a name="l00040"></a><span class="lineno"><a class="line" href="algorithm_8hpp.html#a94e02c4c9fbf63aaf34a8313c6b9ea25">   40</a></span>&#160;std::vector&lt;component::node_ptr&gt; <a class="code" href="algorithm_8hpp.html#a94e02c4c9fbf63aaf34a8313c6b9ea25">topological_sort</a>(<a class="code" href="classbn_1_1network.html">network&lt;RepresentMethod&gt;</a> <span class="keyword">const</span>&amp; g)</div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;{</div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;    std::vector&lt;component::node_ptr&gt; result;</div><div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;    result.reserve(g.<a class="code" href="classbn_1_1network.html#ac1cbce2d87b6a6aaa76432e8b63bf15d">all_node</a>().size());</div><div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;</div><div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;    <span class="comment">// 0: no visit</span></div><div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;    <span class="comment">// 1: temporary</span></div><div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;    <span class="comment">// 2: permanent</span></div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;    std::unordered_map&lt;component::node_ptr, std::uint_fast8_t&gt; marks;</div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;    <span class="keywordflow">for</span>(<span class="keyword">auto</span> <span class="keyword">const</span>&amp; n : g.<a class="code" href="classbn_1_1network.html#ac1cbce2d87b6a6aaa76432e8b63bf15d">all_node</a>())</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;        marks[n] = 0;</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;</div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;    <span class="comment">// return true if a sort series of given node and its children is found;</span></div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;    <span class="comment">// return false if it is not found.</span></div><div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;    <span class="keyword">auto</span> visit = <a class="code" href="algorithm_8hpp.html#ab6d3fea4f9b73d4d4b75792c8dac735b">recursive</a>(</div><div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;        [&amp;marks, &amp;g, &amp;result](<span class="keyword">auto</span> visit, component::node_ptr <span class="keyword">const</span>&amp; n) -&gt; <span class="keywordtype">bool</span></div><div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;        {</div><div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;            <span class="keywordflow">if</span>(marks[n] == 1) <span class="keywordflow">return</span> <span class="keyword">false</span>; <span class="comment">// the network is not DAG.</span></div><div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;            <span class="keywordflow">else</span> <span class="keywordflow">if</span>(marks[n] == 0)</div><div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;            {</div><div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;                marks[n] = 1;</div><div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;                <span class="keywordflow">for</span>(<span class="keyword">auto</span> <span class="keyword">const</span>&amp; child : g.<a class="code" href="classbn_1_1network.html#ac1d37fde77b3deb984b552ef7cec7f52">child_nodes</a>(n))</div><div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;                    <span class="keywordflow">if</span>(!visit(visit, child)) <span class="keywordflow">return</span> <span class="keyword">false</span>;</div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;                marks[n] = 2;</div><div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;                result.push_back(n);</div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;            }</div><div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;</div><div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;            <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;        });</div><div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;</div><div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;    <span class="keywordflow">for</span>(<span class="keyword">auto</span> <span class="keyword">const</span>&amp; n : g.<a class="code" href="classbn_1_1network.html#ac1cbce2d87b6a6aaa76432e8b63bf15d">all_node</a>())</div><div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;        <span class="keywordflow">if</span>(marks[n] == 0)</div><div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;            <span class="keywordflow">if</span>(!visit(n)) <span class="keywordflow">break</span>;</div><div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;</div><div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;    <span class="keywordflow">return</span> result;</div><div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;}</div><div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;</div><div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;</div><div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;<span class="keyword">template</span>&lt;<span class="keyword">class</span> RepresentMethod&gt;</div><div class="line"><a name="l00085"></a><span class="lineno"><a class="line" href="algorithm_8hpp.html#a33dbf558d124350ecc690c06b689eb99">   85</a></span>&#160;<span class="keywordtype">bool</span> <a class="code" href="algorithm_8hpp.html#a33dbf558d124350ecc690c06b689eb99">is_dag</a>(<a class="code" href="classbn_1_1network.html">network&lt;RepresentMethod&gt;</a> <span class="keyword">const</span>&amp; g)</div><div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;{</div><div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;    <span class="keywordflow">return</span> <a class="code" href="algorithm_8hpp.html#a94e02c4c9fbf63aaf34a8313c6b9ea25">topological_sort</a>(g).size() == g.<a class="code" href="classbn_1_1network.html#ac1cbce2d87b6a6aaa76432e8b63bf15d">all_node</a>().size();</div><div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;}</div><div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;</div><div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;} <span class="comment">// namespace bn</span></div><div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;</div><div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;<span class="preprocessor">#endif // BAYESIAN_NETWORKS_ALGORITHM_HPP</span></div><div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;</div><div class="ttc" id="algorithm_8hpp_html_ab6d3fea4f9b73d4d4b75792c8dac735b"><div class="ttname"><a href="algorithm_8hpp.html#ab6d3fea4f9b73d4d4b75792c8dac735b">bn::recursive</a></div><div class="ttdeci">auto recursive(F f)</div><div class="ttdoc">Generate a recursive function of the given function. </div><div class="ttdef"><b>Definition:</b> algorithm.hpp:25</div></div>
<div class="ttc" id="namespacebn_html"><div class="ttname"><a href="namespacebn.html">bn</a></div><div class="ttdef"><b>Definition:</b> algorithm.hpp:16</div></div>
<div class="ttc" id="algorithm_8hpp_html_a94e02c4c9fbf63aaf34a8313c6b9ea25"><div class="ttname"><a href="algorithm_8hpp.html#a94e02c4c9fbf63aaf34a8313c6b9ea25">bn::topological_sort</a></div><div class="ttdeci">std::vector&lt; component::node_ptr &gt; topological_sort(network&lt; RepresentMethod &gt; const &amp;g)</div><div class="ttdoc">Perform topological sort for given graph (however, a reversed series can be obtain). </div><div class="ttdef"><b>Definition:</b> algorithm.hpp:40</div></div>
<div class="ttc" id="algorithm_8hpp_html_a33dbf558d124350ecc690c06b689eb99"><div class="ttname"><a href="algorithm_8hpp.html#a33dbf558d124350ecc690c06b689eb99">bn::is_dag</a></div><div class="ttdeci">bool is_dag(network&lt; RepresentMethod &gt; const &amp;g)</div><div class="ttdoc">Judge that given graph is directed acyclic graph (DAG). </div><div class="ttdef"><b>Definition:</b> algorithm.hpp:85</div></div>
<div class="ttc" id="classbn_1_1network_html"><div class="ttname"><a href="classbn_1_1network.html">bn::network</a></div><div class="ttdoc">Provide a functions for each graph representing class. </div><div class="ttdef"><b>Definition:</b> network.hpp:22</div></div>
<div class="ttc" id="component_8hpp_html"><div class="ttname"><a href="component_8hpp.html">component.hpp</a></div><div class="ttdoc">Implementations of important elements of bn::network. </div></div>
<div class="ttc" id="network_8hpp_html"><div class="ttname"><a href="network_8hpp.html">network.hpp</a></div><div class="ttdoc">Provide a functions for each graph representing class. </div></div>
<div class="ttc" id="classbn_1_1network_html_ac1cbce2d87b6a6aaa76432e8b63bf15d"><div class="ttname"><a href="classbn_1_1network.html#ac1cbce2d87b6a6aaa76432e8b63bf15d">bn::network::all_node</a></div><div class="ttdeci">std::vector&lt; node_ptr &gt; all_node() const</div><div class="ttdoc">Obtain all node pointers. </div><div class="ttdef"><b>Definition:</b> network.hpp:251</div></div>
<div class="ttc" id="classbn_1_1network_html_ac1d37fde77b3deb984b552ef7cec7f52"><div class="ttname"><a href="classbn_1_1network.html#ac1d37fde77b3deb984b552ef7cec7f52">bn::network::child_nodes</a></div><div class="ttdeci">std::vector&lt; node_ptr &gt; child_nodes(node_ptr const &amp;node) const</div><div class="ttdoc">Obtain a list of all nodes which are child of the parent (argument). </div><div class="ttdef"><b>Definition:</b> network.hpp:245</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.14
</small></address>
</body>
</html>
